﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NotifyIconService.cs" company="WordSteps.com">
//   All rights reserved
// </copyright>
// <summary>
//   Defines the NotifyIconService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RapidDict.Modules.ContextTrayMenuService.Services
{
    using System;
    using System.Reflection;
    using System.Windows.Forms;
    using RapidDict.Infrastructure.Interfaces;

    public class NotifyIconService
    {
        private IAppViewService appViewService;
        private IContextTrayMenuService contextTrayMenuService;
        private NotifyIcon notifyIcon;
        
        public NotifyIconService(IAppViewService appViewService, IContextTrayMenuService contextTrayMenuService)
        {
            this.appViewService = appViewService;
            this.contextTrayMenuService = contextTrayMenuService;
            this.InitializeNotifyIcon();

            this.AddHideShowItem();
            this.AddExitItem();
        }

        private void InitializeNotifyIcon()
        {
            this.notifyIcon =
              new NotifyIcon
              {
                  Icon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location),
                  Visible = true,
                  ContextMenuStrip = this.contextTrayMenuService.GetContextTrayMenu()
              };

            this.notifyIcon.DoubleClick += this.NotifyIcon_DoubleClick;
        }

        private void NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.appViewService.ShowApp();
        }

        private void AddHideShowItem()
        {
            var menuExit = new ToolStripMenuItem
            {
                Text = "Exit"
            };
            menuExit.Click += this.menuExit_Click;
            this.contextTrayMenuService.AddMenuItem(menuExit);
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            this.appViewService.CloseApp();
            this.notifyIcon.Visible = false;
        }

        private void AddExitItem()
        {
            var menuHideShow = new ToolStripMenuItem
            {
                Text = "Hide / Show"
            };

            menuHideShow.Click += this.menuHideShow_Click;
            this.contextTrayMenuService.AddMenuItem(menuHideShow);
        }

        public void menuHideShow_Click(object sender, EventArgs e)
        {
            this.appViewService.ChangeVisibility();
        }
    }
}
